#include<cstdio>//uncle-lu
#include<cmath>
#include<algorithm>
template<class T>void read(T &x)
{
	x=0;int f=0;char ch=getchar();
	while(ch<'0'||ch>'9') { f|=(ch=='-'); ch=getchar(); }
	while(ch<='9'&&ch>='0') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
	x = f ? -x : x;
	return ;
}

struct node{
	int left, right, length;
};

node line[20010];
int n;
int F[20010][2];

int main()
{
	read(n);
	for (int i = 1; i <= n; i++) 
	{
		read(line[i].left);read(line[i].right);
		line[i].length = line[i].right - line[i].left;
	}

	F[1][0] = line[1].right + line[1].length - 1;
	F[1][1] = line[1].right - 1;
	for (int i = 2; i <= n; i++) 
	{
		F[i][0] = std::min(F[i-1][0] + abs(line[i-1].left - line[i].right) + line[i].length, F[i-1][1] + abs(line[i-1].right - line[i].right) + line[i].length) + 1;
		F[i][1] = std::min(F[i-1][0] + abs(line[i-1].left - line[i].left) + line[i].length, F[i-1][1] + abs(line[i-1].right - line[i].left) + line[i].length ) + 1;
	}

	printf("%d", std::min(F[n][1] + abs(line[n].right - n), F[n][0] + abs(line[n].left - n)));

	return 0;
}
